﻿#region Copyright (c) 2013-04, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using System.Threading.Tasks;
using Amarok.Agents.Resources;
using NUnit.Framework;


namespace Amarok.Agents
{
	[TestFixture]
	public class Test_DefaultAgentFactory
	{
		[MessageBusConnected]
		private class MockAgent : Agent
		{
			public MockAgent(AgentEnvironment environment, AgentOptions options)
				: base(environment, options)
			{
			}

			public new AgentEnvironment Environment
			{
				get
				{
					return base.Environment;
				}
			}

			public new AgentOptions Options
			{
				get
				{
					return base.Options;
				}
			}
		}

		private class MockAgentWithoutMessageBusConnectedAttribute : Agent
		{
			public MockAgentWithoutMessageBusConnectedAttribute(AgentEnvironment environment, AgentOptions options)
				: base(environment, options)
			{
			}

			public new AgentEnvironment Environment
			{
				get
				{
					return base.Environment;
				}
			}

			public new AgentOptions Options
			{
				get
				{
					return base.Options;
				}
			}
		}

		private class AgentWithInvalidConstructor : Agent
		{
			public AgentWithInvalidConstructor()
				: base(null, null)
			{
			}
		}

		private class AgentWithExceptionInConstructor : Agent
		{
			public AgentWithExceptionInConstructor(AgentEnvironment environment, AgentOptions options)
				: base(environment, options)
			{
				throw new ApplicationException("CTR");
			}
		}

		private class StubEnvironment : AgentEnvironment
		{
		}

		private class StubOptions : AgentOptions
		{
		}

		private class MockMessageBus : IMessageBus
		{
			public Int32 ConnectCalled;
			public IAgent ConnectArgument;

			public ICompletable Connect(IAgent agent)
			{
				ConnectArgument = agent;
				ConnectCalled++;

				return new DummyCompletable();
			}

			public ICompletable Connect(Object label, IAgent agent)
			{
				throw new NotImplementedException();
			}

			public Task Completion
			{
				get
				{
					throw new NotImplementedException();
				}
			}

			public void Complete(bool discardAvailableMessages = false)
			{
				throw new NotImplementedException();
			}

			public void Dispose()
			{
				throw new NotImplementedException();
			}

			public ICompletable Subscribe(Type messageType, AgentReference subscriber, Predicate<Message> messageFilter = null)
			{
				throw new NotImplementedException();
			}

			public ICompletable Subscribe(Type messageType, Action<Message> subscriber, Predicate<Message> messageFilter = null)
			{
				throw new NotImplementedException();
			}

			public void Publish(Message message)
			{
				throw new NotImplementedException();
			}
		}

		private class StubRuntimeEnvironment : RuntimeEnvironment
		{
			public StubRuntimeEnvironment(IRuntime runtime, IMessageBus messageBus)
				: base(runtime, messageBus)
			{
			}
		}


		// --- TESTS ---


		[Test]
		public void Test_SpawnAgent()
		{
			var environment = new StubEnvironment();
			var options = new StubOptions();
			var factory = new DefaultAgentFactory();

			var agent = (MockAgent)factory.SpawnAgent(
				typeof(MockAgent),
				environment,
				options);

			Assert.That(agent, Is.Not.Null);
			Assert.That(agent.Environment, Is.SameAs(environment));
			Assert.That(agent.Options, Is.SameAs(options));
		}

		[Test]
		public void Test_SpawnAgent_NullType()
		{
			var environment = new StubEnvironment();
			var options = new StubOptions();
			var factory = new DefaultAgentFactory();

			Assert.Throws<ArgumentNullException>(() =>
				{
					var agent = factory.SpawnAgent(
						null,
						environment,
						options);
				});
		}

		[Test]
		public void Test_SpawnAgent_NullEnvironment()
		{
			var options = new StubOptions();
			var factory = new DefaultAgentFactory();

			Assert.Throws<ArgumentNullException>(() =>
				{
					var agent = factory.SpawnAgent(
						typeof(MockAgent),
						null,
						options);
				});
		}

		[Test]
		public void Test_SpawnAgent_NullOptions()
		{
			var environment = new StubEnvironment();
			var factory = new DefaultAgentFactory();

			Assert.Throws<ArgumentNullException>(() =>
				{
					var agent = factory.SpawnAgent(
						typeof(MockAgent),
						environment,
						null);
				});
		}

		[Test]
		public void Test_SpawnAgent_NonAgentType()
		{
			var environment = new StubEnvironment();
			var options = new StubOptions();
			var factory = new DefaultAgentFactory();

			Assert.Throws<ArgumentException>(() =>
				{
					var agent = factory.SpawnAgent(
						typeof(Version),
						environment,
						options);
				});
		}

		[Test]
		public void Test_SpawnAgent_TypeWithInvalidConstructor()
		{
			var environment = new StubEnvironment();
			var options = new StubOptions();
			var factory = new DefaultAgentFactory();

			var exception = Assert.Throws<AgentSpawnException>(() =>
				{
					var agent = factory.SpawnAgent(
						typeof(AgentWithInvalidConstructor),
						environment,
						options);
				});

			Assert.That(exception.Message, Is.EqualTo(AgentResources.AgentTypeDoesNotProvideAgentConstructor));
		}

		[Test]
		public void Test_SpawnAgent_TypeWithExceptionInConstructor()
		{
			var environment = new StubEnvironment();
			var options = new StubOptions();
			var factory = new DefaultAgentFactory();

			var exception = Assert.Throws<AgentSpawnException>(() =>
				{
					var agent = factory.SpawnAgent(
						typeof(AgentWithExceptionInConstructor),
						environment,
						options);
				});

			Assert.That(exception.Message, Is.EqualTo(AgentResources.AgentSpawnFailure));
		}

		[Test]
		public void Test_SpawnAgent_RuntimeEnvironment()
		{
			var runtime = new DummyRuntime();
			var messageBus = new MockMessageBus();
			var environment = new StubRuntimeEnvironment(runtime, messageBus);
			var options = new StubOptions();
			var factory = new DefaultAgentFactory();

			var agent = (MockAgent)factory.SpawnAgent(
				typeof(MockAgent),
				environment,
				options);

			Assert.That(agent, Is.Not.Null);
			Assert.That(agent.Environment, Is.SameAs(environment));
			Assert.That(agent.Options, Is.SameAs(options));
			Assert.That(messageBus.ConnectCalled, Is.EqualTo(1));
			Assert.That(messageBus.ConnectArgument, Is.SameAs(agent));
		}

		[Test]
		public void Test_SpawnAgent_RuntimeEnvironment_AgentWithoutMessageBusConnectedAttribute()
		{
			var runtime = new DummyRuntime();
			var messageBus = new MockMessageBus();
			var environment = new StubRuntimeEnvironment(runtime, messageBus);
			var options = new StubOptions();
			var factory = new DefaultAgentFactory();

			var agent = (MockAgentWithoutMessageBusConnectedAttribute)factory.SpawnAgent(
				typeof(MockAgentWithoutMessageBusConnectedAttribute),
				environment,
				options);

			Assert.That(agent, Is.Not.Null);
			Assert.That(agent.Environment, Is.SameAs(environment));
			Assert.That(agent.Options, Is.SameAs(options));
			Assert.That(messageBus.ConnectCalled, Is.EqualTo(0));
		}

	}
}

#endif
